﻿using RpcHelper;
using RpcModel;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Helper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
    internal class ServerTransmitConfigDAL : SqlBasicClass
    {
        public ServerTransmitConfigDAL() : base("ServerTransmitConfig")
        {

        }
        public void Set(long id, TransmitSet set)
        {
            if (!base.Update<TransmitSetParam, long>(set, "Id", id))
            {
                throw new ErrorException("rpc.store.transmit.set.fail");
            }
        }
        public void SetIsEnable(long id, bool isEnable)
        {
            if (!base.Update<long>(new ISqlSetColumn[] {
                new SqlSetColumn("IsEnable", System.Data.SqlDbType.Bit){Value=isEnable}
            }, "Id", id))
            {
                throw new ErrorException("rpc.store.transmit.set.fail");
            }
        }
        public long Add(ServerTransmitConfig add)
        {
            base.Insert(add, out long id);
            return id;
        }
        public ServerTransmitConfig Get(long id)
        {
            return this.GetRow<long, ServerTransmitConfig>("Id", id);
        }

        public TransmitBasicConfig[] Query(TransmitQueryParam query, IBasicPage paging, out long count)
        {
            paging.InitOrderBy("Id", true);
            return base.Query<TransmitBasicConfig>(paging.OrderBy, paging.Index, paging.Size, out count, query.ToWhere());
        }
        public void Delete(long id)
        {
            if (!base.Drop<long>("Id", id))
            {
                throw new ErrorException("rpc.store.transmit.drop.fail");
            }
        }
        public bool CheckRepeat(long rpcMerId, long systemType, string scheme)
        {
            return base.CheckIsExists(new ISqlWhere[] { 
                new SqlWhere("RpcMerId", System.Data.SqlDbType.BigInt){Value=rpcMerId},
                new SqlWhere("SystemTypeId", System.Data.SqlDbType.BigInt){Value=systemType},
                new SqlWhere("Scheme", System.Data.SqlDbType.VarChar,50){Value=scheme}
            });
        }

        public bool CheckRepeat(long rpcMerId, string scheme, long severId)
        {
            return base.CheckIsExists(new ISqlWhere[] {
                new SqlWhere("RpcMerId", System.Data.SqlDbType.BigInt){Value=rpcMerId},
                new SqlWhere("SeverId", System.Data.SqlDbType.BigInt){Value=severId},
                new SqlWhere("Scheme", System.Data.SqlDbType.VarChar,50){Value=scheme}
            });
        }
    }
}
